package org.gap.wizards.world.actions;

import java.awt.Point;

import net.slashie.libjcsi.CharKey;

import org.gap.wizards.views.ViewPlatform;
import org.gap.wizards.views.ViewPlatform.Color;
import org.gap.wizards.world.Movement;

public class TargetSelector {
	
	private final ViewPlatform console;
	private char lastKeyPressed;

	public TargetSelector(ViewPlatform console) {
		this.console = console;
	}

	public Point findTarget(Point targetLocation) {
		console.saveBuffer();
		CharKey key = console.getKeyPressed();
		char pressed = key.toString().charAt(0);
		while (Movement.isValid(pressed)) {
			console.restore();
			Movement direction = Movement.from(pressed);
			targetLocation = direction.adjustLocation(targetLocation);
			char symbol = console.peekSymbol(targetLocation);
			Color color = console.peekColor(targetLocation);
			console.setBackground(Color.YELLOW_GREEN);
			console.print(targetLocation.x, targetLocation.y, color, symbol);
			console.setBackground(Color.BLACK);
			console.refresh();
			key = console.getKeyPressed();
			pressed = key.toString().charAt(0);
		}
		
		lastKeyPressed = pressed;
		return targetLocation;
	}

	public char lastKeyPressed() {
		return lastKeyPressed;
	}

}
